package com.pomelo.tool.controller;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageRowBounds;
import com.pomelo.base.core.bean.PageParam;
import com.pomelo.base.core.bean.PageResultBean;
import com.pomelo.base.core.util.CommonUtil;
import com.pomelo.base.core.util.DbUtil;
import com.pomelo.base.core.util.HttpUtil;
import com.pomelo.base.core.util.StringUtil;

/**
 * 人事应用SVN操作控制器
 * 
 * @author pot
 *
 */
@Controller
@RequestMapping("/hrsvn")
public class HrappSvnController {
	
	/**
	 * 查询所有的人事app
	 * 
	 * @author pot
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryAppList.do") @ResponseBody public PageResultBean queryAppList() throws Exception {
		String sql = "select * from T_HRMS_APP A LEFT JOIN (SELECT count(wid) AS count,APPID AS APPID_ from "
				+ "T_HRMS_APP_PATCH WHERE SJBZ='0' group by APPID) B on A.APPID=B.APPID_";
        PageParam pageParam = HttpUtil.getPageParam();
        PageRowBounds page = new PageRowBounds(pageParam.getPageIndex(), pageParam.getPageSize());
        List<Map<String, Object>> rows = DbUtil.query(sql, new Object[] {}, page);
        PageResultBean pageResult = new PageResultBean();
        pageResult.setPageSize(pageParam.getPageSize());
        pageResult.setPageIndex(pageParam.getPageIndex());
        pageResult.setRows(rows);
        pageResult.setTotal(page.getTotal());
        return pageResult;
	}
	
	/**
	 * 查询对应app下的所有补丁
	 * 
	 * @author pot
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryPatchList.do") @ResponseBody public PageResultBean queryPatchList() throws Exception {
		String appid = HttpUtil.getParameter("appid");
		String version = HttpUtil.getParameter("version");
		String sql = "SELECT * FROM T_HRMS_APP_PATCH WHERE APPID = ? AND VERSION = ?";
        PageParam pageParam = HttpUtil.getPageParam();
        PageRowBounds page = new PageRowBounds(pageParam.getPageIndex(), pageParam.getPageSize());
        List<Map<String, Object>> rows = DbUtil.query(sql, new Object[] {appid,version}, page);
        //非定制版本
        if (appid.charAt(appid.length()-2) != '$') {
        	String customAppId = appid + "$A";
        	//最多循环10-20次 一般10次以内
        	addState(version, rows, customAppId);
        } else {//定制版本
        	String appId = appid.substring(0, appid.length()-2);
        	//最多循环10-20次 一般10次以内
        	addState(version, rows, appId);
        }
        PageResultBean pageResult = new PageResultBean();
        pageResult.setPageSize(pageParam.getPageSize());
        pageResult.setPageIndex(pageParam.getPageIndex());
        pageResult.setRows(rows);
        pageResult.setTotal(page.getTotal());
        return pageResult;
	}

	/**
	 * 添加补丁状态 可升级/不可升级
	 * 
	 * @author pot
	 * @param version 应用版本
	 * @param rows 应用List
	 * @param appId 应用id
	 * @throws Exception
	 */
	private void addState(String version, List<Map<String, Object>> rows, String appId) throws Exception {
		String queryAnotherAppSql = "SELECT APPID FROM T_HRMS_APP WHERE APPID = ? AND VERSION = ?";
		Map<String, Object> existAnotherAppFlag = DbUtil.queryRow(queryAnotherAppSql, new Object[] {appId,version});
		for (Map<String, Object> row : rows) {
			if (!CommonUtil.isEmptyMap(existAnotherAppFlag)) {
				String zbwid = StringUtil.toStr(row.get("WID"));
				String anotherAppWidSql = "SELECT * FROM T_HRMS_APP_PATCH WHERE APPID = ? AND VERSION = ?";
				Map<String, Object> anotherAppWidMap = DbUtil.queryRow(anotherAppWidSql, new Object[] {appId,version});
				String anotherAppWid = StringUtil.toStr(anotherAppWidMap.get("WID"));
				String queryAppPatchSql = "SELECT PATH FROM T_HRMS_APP_PATCHXQ WHERE ZBWID = ?";
				List<Map<String, Object>> anotherAppPatchList = DbUtil.query(queryAppPatchSql, new Object[] {anotherAppWid});
				String paths = "";
				for (Map<String, Object> anotherAppPatchInfo : anotherAppPatchList) {
					String anotherAppPatchPath = StringUtil.toStr(anotherAppPatchInfo.get("PATH"));
					paths = paths + anotherAppPatchPath + ";";
				}
				List<Map<String, Object>> AppPatchList = DbUtil.query(queryAppPatchSql, new Object[] {zbwid});
				for (Map<String, Object> AppPatchInfo : AppPatchList) {
					String appPatchPath = StringUtil.toStr(AppPatchInfo.get("PATH"));
					if (paths.contains(appPatchPath)) {
						row.put("STATE", "-1");
						break;
					} else {
						row.put("STATE", "100");
					}
				}
			} else {
				row.put("STATE", "100");
			}
		}
	}
	
	/**
	 * 查询当前补丁的修改路径详情信息
	 * 
	 * @author pot
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryPatchDetail.do") @ResponseBody public PageResultBean queryPatchDetail() throws Exception {
		String zbwid = HttpUtil.getParameter("zbwid");
		String sql = "SELECT * FROM T_HRMS_APP_PATCHXQ WHERE ZBWID = ?";
        PageParam pageParam = HttpUtil.getPageParam();
        PageRowBounds page = new PageRowBounds(pageParam.getPageIndex(), pageParam.getPageSize());
        List<Map<String, Object>> rows = DbUtil.query(sql, new Object[] {zbwid}, page);
        PageResultBean pageResult = new PageResultBean();
        pageResult.setPageSize(pageParam.getPageSize());
        pageResult.setPageIndex(pageParam.getPageIndex());
        pageResult.setRows(rows);
        pageResult.setTotal(page.getTotal());
        return pageResult;
	}
	/**************************************************************************/
	/**
	 * 学校应用
	 */
	/**************************************************************************/

	/**
	 * 查询所有学校的app
	 * 
	 * @author pot
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/querySchoolAppList.do") @ResponseBody public PageResultBean querySchoolAppList() throws Exception {
		String sql = "select * from T_HRMS_APPSCHOOL A LEFT JOIN (SELECT count(wid) AS count,APPID AS APPID_"
				+ " from T_HRMS_APP_PATCH WHERE SJBZ='0' group by APPID) B on A.APPID=B.APPID_";
        PageParam pageParam = HttpUtil.getPageParam();
        PageRowBounds page = new PageRowBounds(pageParam.getPageIndex(), pageParam.getPageSize());
        List<Map<String, Object>> rows = DbUtil.query(sql, new Object[] {}, page);
        PageResultBean pageResult = new PageResultBean();
        pageResult.setPageSize(pageParam.getPageSize());
        pageResult.setPageIndex(pageParam.getPageIndex());
        pageResult.setRows(rows);
        pageResult.setTotal(page.getTotal());
        return pageResult;
	}
	
	/**
	 * 查询各学校app下的补丁
	 * 
	 * @author pot
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/querySchoolPatchList.do") @ResponseBody public PageResultBean querySchoolPatchList() throws Exception {
		String appid = HttpUtil.getParameter("appid");
		String version = HttpUtil.getParameter("version");
		String school = HttpUtil.getParameter("school");
		String sql = "SELECT * FROM T_HRMS_APP_PATCH WHERE APPID = ? AND VERSION = ?";
        PageParam pageParam = HttpUtil.getPageParam();
        PageRowBounds page = new PageRowBounds(pageParam.getPageIndex(), pageParam.getPageSize());
        List<Map<String, Object>> rows = DbUtil.query(sql, new Object[] {appid,version,}, page);
        if (appid.charAt(appid.length()-2) != '$') {
        	String customAppId = appid + "$A";
        	//最多循环10-20次 一般10次以内
        	addState1(school, version, rows, customAppId);
        } else {//定制版本
        	String appId = appid.substring(0, appid.length()-2);
        	//最多循环10-20次 一般10次以内
        	addState1(school, version, rows, appId);
        }
        PageResultBean pageResult = new PageResultBean();
        pageResult.setPageSize(pageParam.getPageSize());
        pageResult.setPageIndex(pageParam.getPageIndex());
        pageResult.setRows(rows);
        pageResult.setTotal(page.getTotal());
        return pageResult;
	}
	
	/**
	 * 查看补丁修改详情
	 * 
	 * @author pot
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/querySchoolPatchDetail.do") @ResponseBody public PageResultBean querySchoolPatchDetail() throws Exception {
		String zbwid = HttpUtil.getParameter("zbwid");
		String sql = "SELECT * FROM T_HRMS_APP_PATCHXQ WHERE ZBWID = ?";
        PageParam pageParam = HttpUtil.getPageParam();
        PageRowBounds page = new PageRowBounds(pageParam.getPageIndex(), pageParam.getPageSize());
        List<Map<String, Object>> rows = DbUtil.query(sql, new Object[] {zbwid}, page);
        PageResultBean pageResult = new PageResultBean();
        pageResult.setPageSize(pageParam.getPageSize());
        pageResult.setPageIndex(pageParam.getPageIndex());
        pageResult.setRows(rows);
        pageResult.setTotal(page.getTotal());
        return pageResult;
	}
	
	private void addState1(String school, String version, List<Map<String, Object>> rows, String appId) throws Exception {
		String queryAnotherAppSql = "SELECT APPID FROM T_HRMS_APPSCHOOL WHERE APPID = ? AND VERSION = ? AND SCHOOL = ?";
		Map<String, Object> existAnotherAppFlag = DbUtil.queryRow(queryAnotherAppSql, new Object[] {appId,version,school});
		for (Map<String, Object> row : rows) {
			if (!CommonUtil.isEmptyMap(existAnotherAppFlag)) {
				String zbwid = StringUtil.toStr(row.get("WID"));
				String anotherAppWidSql = "SELECT * FROM T_HRMS_APP_PATCH WHERE APPID = ? AND VERSION = ?";
				Map<String, Object> anotherAppWidMap = DbUtil.queryRow(anotherAppWidSql, new Object[] {appId,version});
				String anotherAppWid = StringUtil.toStr(anotherAppWidMap.get("WID"));
				String queryAppPatchSql = "SELECT PATH FROM T_HRMS_APP_PATCHXQ WHERE ZBWID = ?";
				List<Map<String, Object>> anotherAppPatchList = DbUtil.query(queryAppPatchSql, new Object[] {anotherAppWid});
				String paths = "";
				for (Map<String, Object> anotherAppPatchInfo : anotherAppPatchList) {
					String anotherAppPatchPath = StringUtil.toStr(anotherAppPatchInfo.get("PATH"));
					paths = paths + anotherAppPatchPath + ";";
				}
				List<Map<String, Object>> AppPatchList = DbUtil.query(queryAppPatchSql, new Object[] {zbwid});
				for (Map<String, Object> AppPatchInfo : AppPatchList) {
					String appPatchPath = StringUtil.toStr(AppPatchInfo.get("PATH"));
					if (paths.contains(appPatchPath)) {
						row.put("STATE", "-1");
						break;
					} else {
						row.put("STATE", "100");
					}
				}
			} else {
				row.put("STATE", "100");
			}
		}
	}
	
}
